# -*- coding: utf-8 -*-
"""
Created on Mon Mar  1 16:34:45 2021

@author: 59567
"""
import sys
sys.path.append('..')
import os
from pandas import read_excel
try:
    from utils import check_path, get_file_list
except:
    from tjdutils.utils import check_path, get_file_list
from pandas import read_pickle
import shutil


def find_trainer(y_name):
    trainer = check_path('../../output/trainer/')
    find_file = 'not_exists'
    if os.path.exists(trainer):
        trainer_files = os.listdir(trainer)
        for file in trainer_files:
            y_trainer_file = os.path.join(trainer, file)
            pkl_files = os.listdir(y_trainer_file)
            pkl_file = pkl_files[0]
            loaded_pkl = read_pickle(os.path.join(y_trainer_file, pkl_file))
            data = dict(loaded_pkl[1][0])
            trainer_y_name = data['y_name']
            print(trainer_y_name)
            if trainer_y_name == y_name:
                find_file = y_trainer_file
    return find_file
            

def del_output_trainer_y(y_name):
    file = find_trainer(y_name)
    if isinstance(file, str):
        print(y_name, 'not exists')
    else:
        if os.path.exists(file):
            shutil.rmtree(file, ignore_errors=True)


def collector_features(dir_path):
    # 返回两类数据,一类是本期用到的数据,第二类是过去用到的数据
    if os.path.exists(dir_path):
        file_names = get_file_list(dir_path, [])
        added_features = []
        needed_features = []
        for name in file_names:
            if 'latest_features' in name:
                print(name)
                features_df = read_excel(name,index_col=0)
                print(features_df)
                added_features += list(features_df.iloc[:,0])
                print('Collecting features %d' %len(added_features))
            elif 'x_names_all' in name:
                needed_df = read_pickle(name)
                temp_features = needed_df.columns.to_list()
                needed_features += temp_features
    added_names = list(set(added_features))
    needed_names = list(set(needed_features))
    return added_names, needed_names


def input_x_pkls():
    path = check_path('../../input/')
    if os.path.exists(path):
        files = get_file_list(path, [])
        output_files = []
        for file in files:
            if '.pkl' in file:
                output_files.append(os.path.split(file)[-1])
    return output_files


def copy_with_dir(basic_dir, from_path): # 复制文件，如果文件夹不存在则创建文件夹，一定是output里面的文件
    from_path = os.path.normpath(from_path)
    #print("from_path", from_path)
    after_output = from_path.split("output")[-1]
    #print("after_output", after_output)
    targets_path = basic_dir + "\\" + "output" + "\\" + after_output
    targets_path_li = os.path.normpath(targets_path).split("\\")
    before_output = os.path.normpath(targets_path).split("output")[0]
    idx = targets_path_li.index("output")
    #print(targets_path_li[idx:])
    for i in range(idx, len(targets_path_li)-1):
        before_output = before_output + "\\" + str(targets_path_li[i])
        if not os.path.exists(before_output):
            os.makedirs(before_output)
            #print(before_output)
    shutil.copy(from_path, targets_path)



    
if __name__ == "__main__":
    #a = find_trainer("PPI")
    path = "D:/春节版/output/to_tjd/"
    c = collector_features(path)
    # feature_pkls = input_x_pkls()